package Web.service.impl;

import Web.constants.SystemCanstants;
import Web.domain.ResponseResult;
import Web.domain.entity.Article;
import Web.domain.entity.Like;
import Web.domain.entity.Praise;
import Web.domain.entity.User;
import Web.mapper.LikeMapper;
import Web.mapper.PraiseMapper;
import Web.service.ArticleService;
import Web.service.LikeService;
import Web.service.UserService;
import Web.utils.BeanCopyUtils;
import Web.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;

@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements LikeService {
    @Autowired
    ArticleService articleService;
    @Autowired
    LikeMapper likeMapper;
    @Autowired
    UserService userService;
    @Override
    public Integer setLike(Like like) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Like::getUserLikeId,like.getUserLikeId());
        lambdaQueryWrapper.eq(Like::getUserId,like.getUserId());
        Like pp=likeMapper.selectOne(lambdaQueryWrapper);
//        Article article=articleService.getById(praise.getUser_id());
//        article.setPhoto_id(praise.getArticle_id());
        Integer result=0;
        if (pp==null){
            result=1;
            save(like);
            return result;
        }
        else{
            result=2;
            removeById(pp.getId());
            return result;
        }
    }

    @Override
    public ResponseResult getLikeUsers(Long id) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Like::getUserId,id);
        Page<Like> page = new Page<>(SystemCanstants.ARTICLE_STATUS_CURRENT,SystemCanstants.ARTICLE_STATUS_SIZE);
        page(page,lambdaQueryWrapper);

        List<Like> likeList = page.getRecords();
        List<LikeUsersVo> likeUsersVos = BeanCopyUtils.copyBeanList(likeList, LikeUsersVo.class);


        return ResponseResult.okResult(likeUsersVos);
    }

    @Override
    public ResponseResult getLikedUsers(Long id) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Like::getUserLikeId,id);
        Page<Like> page = new Page<>(SystemCanstants.ARTICLE_STATUS_CURRENT,SystemCanstants.ARTICLE_STATUS_SIZE);
        page(page,lambdaQueryWrapper);

        List<Like> likeList = page.getRecords();
        List<LikedUsersVo> likedUsersVos = BeanCopyUtils.copyBeanList(likeList, LikedUsersVo.class);


        return ResponseResult.okResult(likedUsersVos);
    }

    @Override
    public ResponseResult getLikeDetail(Long id) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Like::getUserId,id);
        Page<Like> page = new Page<>(SystemCanstants.ARTICLE_STATUS_CURRENT,SystemCanstants.ARTICLE_STATUS_SIZE);
        page(page,lambdaQueryWrapper);

        List<Like> likeList = page.getRecords();
        List<LikeDetailVo> likeDetailVos=new ArrayList<>();
        for(Like like:likeList){
            LikeDetailVo likeDetailVo=new LikeDetailVo();
            likeDetailVo.setId(like.getUserLikeId());
            likeDetailVo.setAvatar(userService.getById(like.getUserLikeId()).getAvatar());
            likeDetailVo.setNicheng(userService.getById(like.getUserLikeId()).getNickName());
            LambdaQueryWrapper<Article> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(Article::getCreateBy,like.getUserLikeId());
            lambdaQueryWrapper1.orderByDesc(Article::getCreateTime);
            List<Article> articles=articleService.list(lambdaQueryWrapper1);
            if (articles.isEmpty());
            else{
                Article article_1=articles.get(0);
                ArticleDetailVo articleDetailVo=BeanCopyUtils.copyBean(article_1,ArticleDetailVo.class);
                likeDetailVo.setArticleDetailVo(articleDetailVo);
            }
            LambdaQueryWrapper<Like> lambdaQueryWrapper2=new LambdaQueryWrapper<>();
            lambdaQueryWrapper2.eq(Like::getUserLikeId,like.getUserLikeId());
            List<Like> likess=likeMapper.selectList(lambdaQueryWrapper2);
            likeDetailVo.setFansnum(likess.size());
            likeDetailVos.add(likeDetailVo);
        }


        return ResponseResult.okResult(likeDetailVos);
    }

    @Override
    public ResponseResult getLikeStatus(Like like) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Like::getUserLikeId,like.getUserLikeId());
        lambdaQueryWrapper.eq(Like::getUserId,like.getUserId());
        Like like1=likeMapper.selectOne(lambdaQueryWrapper);
        LikeStatusVo likeStatusVo=new LikeStatusVo();
        if(like1==null){
            likeStatusVo.setStatus(0);
        }
        else{
            likeStatusVo.setStatus(1);
        }
        return ResponseResult.okResult(likeStatusVo);
    }
}
